package com.fantasy.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

/**
 * 演示登录
 * 
 * @author xiyang
 * 
 */
public class TestLogin {
	public static void main(String[] args) {
		
	}
	
	public static StringBuilder getHtml(int page) throws ClientProtocolException, IOException{
		HttpClient httpclient = HttpClients.createDefault();

		// 设置登录参数
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		formparams.add(new BasicNameValuePair("zt", "bpdzerp"));
		formparams.add(new BasicNameValuePair("userid", "bp024"));
		formparams.add(new BasicNameValuePair("pwd", "bp123"));
		formparams.add(new BasicNameValuePair("dlms", "2"));
		formparams.add(new BasicNameValuePair("Submit", ""));
		//formparams.add(new BasicNameValuePair("usr_name", "bp024"));
		//formparams.add(new BasicNameValuePair("usr_pwd", "bp1243"));
		UrlEncodedFormEntity entity1 = new UrlEncodedFormEntity(formparams, "UTF-8");

		//HttpGet httpgetCookie = new HttpGet("http://biaoping.gnway.cc:8088");
		//HttpResponse response = httpclient.execute(httpgetCookie);

		
		// 新建Http post请求
		HttpPost httppost = new HttpPost("http://biaoping.gnway.cc:8088/desktop/login.php");
		//HttpPost httppost = new HttpPost("http://125.1.0.33/csm/loginValidate");
		//httppost.setHeader("Cookie", set_cookie);
		httppost.setEntity(entity1);
		httppost.setHeader("Host","biaoping.gnway.cc:8088");
		httppost.setHeader("Connection","keep-alive");
		httppost.setHeader("Cache-Control","max-age=0");
		httppost.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		httppost.setHeader("Origin","http://biaoping.gnway.cc:8088");
		httppost.setHeader("Upgrade-Insecure-Requests","1");
		httppost.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36");
		httppost.setHeader("Content-Type","application/x-www-form-urlencoded");
		httppost.setHeader("Referer","http://biaoping.gnway.cc:8088/index.php");
		httppost.setHeader("Accept-Encoding","gzip, deflate");
		httppost.setHeader("Accept-Language","zh-CN,zh;q=0.8");
		//httppost.setHeader("Cookie","PHPSESSID=b32b26954ee5fbb40a1a81bcf6c10385; path=/");
		// 处理请求，得到响应
		HttpResponse response = httpclient.execute(httppost);
		String set_cookie = response.getFirstHeader("Set-Cookie").getValue();

		// 打印Cookie值
		//System.out.println(set_cookie.substring(0, set_cookie.indexOf(";")));

		// 打印返回的结果
		HttpEntity entity = response.getEntity();

		StringBuilder result = new StringBuilder();
		if (entity != null) {
			InputStream instream = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(instream));
			String temp = "";
			while ((temp = br.readLine()) != null) {
				String str = new String(temp.getBytes(), "utf-8");
				result.append(str);
			}
		}
		//System.out.println(result);
		
        // 根据获得的Cookie值，设置头信息，然后发送请求，获得内容 
        HttpGet httpget = new HttpGet("http://biaoping.gnway.cc:8088/system/product_manage_list.php?page=" + page);
        httpget.setHeader("Cookie", set_cookie);
        response = httpclient.execute(httpget);
        entity = response.getEntity();
        result = new StringBuilder();
		if (entity != null) {
			InputStream instream = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(instream));
			String temp = "";
			while ((temp = br.readLine()) != null) {
				String str = new String(temp.getBytes(), "utf-8");
				result.append(str);
			}
		}
		//System.out.println(result);
		return result;
	}
}